package org.terramagnet.mvcframework.jdbc.dialect;

import org.terramagnet.mvcframework.jdbc.Dialect;

/**
 * MySql数据库方言.
 *
 * @author lip
 */
public class MySQLDialect implements Dialect {

    @Override
    public String toCountSelect(String select) throws IllegalArgumentException {
        int i = select.indexOf("from");
        if (i < 0) {
            throw new IllegalArgumentException("分页查询语句解析失败：" + select);
        }
        int j = select.indexOf("limit");
        StringBuilder builder = new StringBuilder().append("select count(1) ");
        if (j > 0) {
            builder.append(select.substring(i, j));
        } else {
            builder.append(select.substring(i));
        }
        return builder.toString();
    }

    @Override
    public String toPaginalSelect(String select) throws IllegalArgumentException {
        if (select.endsWith(";")) {
            select = select.substring(0, select.length() - 1);
        }
        return select + " limit ?,?";
    }

}
